package com.nl.controller.xuyi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.bean.DictInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.global.GlobalConstants;
import com.nl.controller.xuyi.bean.linepointcheckbo.*;
import com.nl.controller.xuyi.mapper.XuYiLinePointCheckMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangheng
 * @date 17:17 2020/9/27
 */
@Service
public class XuYiLinePointCheckService {
	@Autowired
	private XuYiLinePointCheckMapper xuYiLinePointCheckMapper;
	@Autowired
	private CommonService commonService;

	public Response qryLinePointCheckInfo(QryLinePointCheckInfoReq req){
		List<LinePointCheckPo> checkPos = new ArrayList<>();
		Map<String,String> typeMap = new HashMap<>();
		try {
			PageHelper.startPage(req.getPage(), req.getLimit());
			checkPos = xuYiLinePointCheckMapper.qryLinePointCheckInfo(req.getCheckState(),req.getCheckType(),"");
			List<DictInfo> dictInfos = commonService.qryDict("1001");
			for (DictInfo dictInfo : dictInfos){
				typeMap.put(dictInfo.getValue(),dictInfo.getDictName());
			}
			for (LinePointCheckPo linePointCheckPo:checkPos){
				linePointCheckPo.setTypeName(typeMap.get(linePointCheckPo.getType()));

			}
		} catch (Exception e){
			throw new SmsException("查询线路点位审核信息异常："+e.getMessage());
		}
		PageInfo<LinePointCheckPo> pageInfo = new PageInfo<>(checkPos);
		return  ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
	}

	public BaseResult<Object> qryCheckInfoWithId(String checkId){
		BaseResult<Object> result = BaseResult.newFailInstance();
		Map<String,String> typeMap = new HashMap<>();
		try {
			List<LinePointCheckPo> linePointCheckPos = xuYiLinePointCheckMapper.qryLinePointCheckInfo("", "", checkId);
			if (!CollectionUtils.isEmpty(linePointCheckPos)){
				LinePointCheckPo linePointCheckPo = linePointCheckPos.get(0);
				List<DictInfo> dictInfos = commonService.qryDict("1001");
				for (DictInfo dictInfo : dictInfos){
					typeMap.put(dictInfo.getValue(),dictInfo.getDictName());
				}
				linePointCheckPo.setTypeName(typeMap.get(linePointCheckPo.getType()));
				result.setRetCode(GlobalConstants.SUCCESS_CODE);
				result.setData(linePointCheckPo);
			}
		} catch (Exception e) {
			throw new SmsException("查询线路点位审核信息异常："+e.getMessage());
		}
		return  result;
	}

	public BaseResult<Object> checkLinePoint(LinePointCheckPo linePointCheckPo){
		BaseResult<Object> result = BaseResult.newFailInstance();
		String checkState = linePointCheckPo.getCheckState();
		try {
			//2 审核不通过
			if ("2".equals(checkState)){
				//更新审核信息表状态
				xuYiLinePointCheckMapper.updateLinePointCheckInfo(linePointCheckPo);
			} else if ("1".equals(checkState)){
				//更新审核信息表状态
				xuYiLinePointCheckMapper.updateLinePointCheckInfo(linePointCheckPo);
				//获取完整审核信息
				LinePointCheckPo checkPo = xuYiLinePointCheckMapper.qryLinePointCheckInfo("", "", linePointCheckPo.getCheckId()).get(0);
				switch (checkPo.getType()){
					//点位新增
					case "1":
						PointInfoPo addPointInfoPo= new PointInfoPo();
						addPointInfoPo.setPointId(checkPo.getPointId());
						addPointInfoPo.setPointName(checkPo.getPointName());
						addPointInfoPo.setBdRange(checkPo.getBdItdu());
						addPointInfoPo.setGpsRange(checkPo.getGpsItdu());
						addPointInfoPo.setRemark(checkPo.getRemark());
						addPointInfoPo.setOperator(checkPo.getOperator());
						addPointInfoPo.setState("1");
						xuYiLinePointCheckMapper.addPoint(addPointInfoPo);
						break;
					//点位修改
					case "2" :
						PointInfoPo pointInfoPo= new PointInfoPo();
						pointInfoPo.setPointId(checkPo.getPointId());
						pointInfoPo.setPointName(checkPo.getPointName());
						pointInfoPo.setBdRange(checkPo.getBdItdu());
						pointInfoPo.setGpsRange(checkPo.getGpsItdu());
						pointInfoPo.setRemark(checkPo.getRemark());
						pointInfoPo.setOperator(checkPo.getOperator());
						xuYiLinePointCheckMapper.updatePoint(pointInfoPo);
						break;
					//点位删除
					case "3" :
						xuYiLinePointCheckMapper.delPoint(checkPo.getRemark(),checkPo.getOperator(),checkPo.getPointId());
						xuYiLinePointCheckMapper.delRelByPoint(checkPo.getPointId(),checkPo.getOperator());
						break;
					//线路新增
					case "4":
						LineInfoPo addLineInfo = new LineInfoPo();
						addLineInfo.setLineId(checkPo.getLineId());
						addLineInfo.setLineName(checkPo.getLineName());
						addLineInfo.setRemark(checkPo.getRemark());
						addLineInfo.setOperator(checkPo.getOperator());
						addLineInfo.setState("1");
						xuYiLinePointCheckMapper.addXuYiLine(addLineInfo);
						addLinePointRel(checkPo);
						break;
					//线路修改
					case "5":
						LineInfoPo updateLineInfo = new LineInfoPo();
						updateLineInfo.setLineId(checkPo.getLineId());
						updateLineInfo.setLineName(checkPo.getLineName());
						updateLineInfo.setRemark(checkPo.getRemark());
						updateLineInfo.setState("1");
						updateLineInfo.setOperator(checkPo.getOperator());
						xuYiLinePointCheckMapper.updateLine(updateLineInfo);
						xuYiLinePointCheckMapper.updateLinePointRel(checkPo.getLineId(),checkPo.getOperator());
						addLinePointRel(checkPo);
						break;
					//线路删除
					case "6":
						xuYiLinePointCheckMapper.updateLinePointRel(checkPo.getLineId(),checkPo.getOperator());
						xuYiLinePointCheckMapper.delLine(checkPo.getLineId(),checkPo.getOperator());
						break;
					default:
						break;
				}
			}
			result.setRetCode(GlobalConstants.SUCCESS_CODE);
		} catch (Exception e) {
			throw  new SmsException("审核线路点位异常："+e.getMessage());
		}
		return result;
	}

	private void addLinePointRel(LinePointCheckPo checkPo) {
		List<LinePointRelPo> updateList = new ArrayList<>();
		String[] pointIds = checkPo.getPointId().split(",");
		for (String pointId : pointIds) {
			LinePointRelPo relPo = new LinePointRelPo();
			relPo.setLineId(checkPo.getLineId());
			relPo.setOperator(checkPo.getOperator());
			relPo.setPointId(pointId);
			relPo.setRemark(checkPo.getRemark());
			relPo.setState("1");
			updateList.add(relPo);
		}
		xuYiLinePointCheckMapper.addLinePointRel(updateList);
	}

}
